package com.weirong.smartvault.backservice.controller;

import com.weirong.smartvault.backservice.controller.fragment_entity.PagesIndex;
import com.weirong.smartvault.backservice.entity_model.*;
import com.weirong.smartvault.backservice.service.ForeignCurrencyRequestService;
import com.weirong.smartvault.common.tools.constant.ThymleafVarName;
import com.weirong.smartvault.dao.*;
import com.weirong.smartvault.entity.*;
import com.weirong.smartvault.backservice.service.DeliveryLogService;
import com.weirong.smartvault.backservice.service.InStorageLogService;
import com.weirong.smartvault.backservice.util.common.DateUtil;
import com.weirong.smartvault.backservice.util.common.ListTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.*;

/**
 * 于2017/11/29由 KFEB4 创建 .
 */
@Controller
@RequestMapping(value = "/currency")
public class Currency {
    @Autowired
    DeliveryLogRepository deliveryLogRepository;
    @Autowired
    DeliveryDetailLogRepository deliveryDetailLogRepository;

    @Autowired
    ForeignCurrencyRequestRepository foreignCurrencyRequestRepository;

    @Autowired
    ForeignCurrencyRequestDetailRepository foreignCurrencyRequestDetailRepository;

    @Autowired
    DeliveryLogService deliveryLogService;

    @Autowired
    InStorageLogRepository inStorageLogRepository;

    @Autowired
    InStorageLogService inStorageLogService;

    @Autowired
    ForeignCurrencyRequestService foreignCurrencyRequestService;
    @Autowired
    InStorageDetailLogRepository inStorageDetailLogRepository;

    @Autowired
    StockRepository stockRepository ;

    @RequestMapping(value = "/delivery", method = RequestMethod.GET)
    public String deliverylog_get(ModelMap model, @RequestParam(value = "page", defaultValue = "0") Integer page,
                          @RequestParam(value = "size", defaultValue = "10") Integer size, @RequestParam(value = "start", defaultValue = "") String s,
                          @RequestParam(value = "end", defaultValue = "") String e) throws ParseException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        Date start = DateUtil.getTodayOrParse(s);
        Date end = DateUtil.getTodayOrParse(e);
        end = DateUtil.getEndDate(end);

        Page<DeliveryLog> deliveryLogs = deliveryLogService.findAllByCreateTimeBetweenWithPageing(start,end,page,size);
        List<DeliveryLogModel> tempDeliveryLogs = ListTool.cast(deliveryLogs,DeliveryLogModel.class);
        List<ListModel> logs = ListTool.cast(tempDeliveryLogs,ListModel.class);
        model.addAttribute(ThymleafVarName.Var.logs, logs);

        model.addAttribute(ThymleafVarName.pages,new PagesIndex(page,deliveryLogs.getTotalPages(),"start",s,"end",e));
        return "currency/delivery";
    }

    @RequestMapping(value = "/delivery/delivery_detail")
    public String delivery_detail(ModelMap model, @RequestParam(value = "id", defaultValue = "") Integer id) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, NoSuchFieldException {
        List<DeliveryDetailLog> deliveryDetailLogs = deliveryDetailLogRepository.findAllByDeliveryId(id);
        Collection<MoneyLogCollectionModel<MoneyLogModel>> logs;
        logs = MoneyLogCollectionModel.makeCollection(ListTool.cast(deliveryDetailLogs,MoneyLogModel.class));
        model.addAttribute(ThymleafVarName.Var.moneyDetail, logs);
        return "currency/delivery/delivery_detail";
    }

    @RequestMapping(value = "/request", method = RequestMethod.GET)
    public String request_get(ModelMap model, @RequestParam(value = "page", defaultValue = "0") Integer page,
                                  @RequestParam(value = "size", defaultValue = "10") Integer size, @RequestParam(value = "start", defaultValue = "") String s,
                                  @RequestParam(value = "end", defaultValue = "") String e) throws ParseException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        Date start = DateUtil.getTodayOrParse(s);
        Date end = DateUtil.getTodayOrParse(e);
        end = DateUtil.getEndDate(end);

        Page<ForeignCurrencyRequest> deliveryLogs = foreignCurrencyRequestService.findAllByCreateTimeBetweenWithPageing(start,end,page,size);
        List<ForeignCurrencyRequestModel> tlogs = ListTool.cast(deliveryLogs,ForeignCurrencyRequestModel.class);
        List<ListModel> logs = ListTool.cast(tlogs,ListModel.class);

        model.addAttribute(ThymleafVarName.Var.logs, logs);

        model.addAttribute(ThymleafVarName.pages,new PagesIndex(page,deliveryLogs.getTotalPages(),"start",s,"end",e));
        return "currency/request";
    }
    @RequestMapping(value = "/request/request_detail")
    public String request_detail(ModelMap model, @RequestParam(value = "id", defaultValue = "") Integer id) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, NoSuchFieldException {
        ForeignCurrencyRequest foreignCurrencyRequest = foreignCurrencyRequestRepository.findOne(id);
        Collection<MoneyLogCollectionModel<MoneyLogModel>> logs;

        logs = MoneyLogCollectionModel.makeCollection(ListTool.cast(foreignCurrencyRequest.getDetails(),MoneyLogModel.class));

        model.addAttribute(ThymleafVarName.Var.moneyDetail, logs);
        return "currency/request/request_detail";
    }

    @RequestMapping(value = "/storage")
    public String storage(ModelMap model, @RequestParam(value = "page", defaultValue = "0") Integer page,
                          @RequestParam(value = "size", defaultValue = "10") Integer size, @RequestParam(value = "start", defaultValue = "") String s,
                          @RequestParam(value = "end", defaultValue = "") String e) throws ParseException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        Date start = DateUtil.getTodayOrParse(s);
        Date end = DateUtil.getTodayOrParse(e);
        if (e.length() == 0) {
            end = DateUtil.getDateNext(start);
        }
        Page<InStorageLog> deliveryLogs = inStorageLogService.findAllByCreateTimeBetweenWithPageing(start,end,page,size);
        List<InStorageLogModel> tlogs = ListTool.cast(deliveryLogs,InStorageLogModel.class);
        List<ListModel> logs = ListTool.cast(tlogs,ListModel.class);
        model.addAttribute(ThymleafVarName.Var.logs, logs);
        model.addAttribute(ThymleafVarName.pages,new PagesIndex(page,deliveryLogs.getTotalPages(),"start",s,"end",e));
        return "currency/storage";
    }

    @RequestMapping(value = "/storage/storage_detail")
    public String storageDetail(ModelMap model, @RequestParam(value = "id", defaultValue = "") Integer id) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, NoSuchFieldException {
        InStorageLog log = inStorageLogRepository.findOne(id);
        List<InStorageDetailLog> list = inStorageDetailLogRepository.findAllByInStorageLog(log);
        Collection<MoneyLogCollectionModel<MoneyLogModel>> logs;

        logs = MoneyLogCollectionModel.makeCollection(ListTool.cast(list,MoneyLogModel.class));

        model.addAttribute(ThymleafVarName.Var.moneyDetail, logs);
        return "currency/storage/storage_detail";
    }
//*
    @RequestMapping(value = "/stock",method = RequestMethod.GET)
    public String stockGet(ModelMap model) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, NoSuchFieldException {
        List<Stock> stocks1 = stockRepository.findAll();
        List<StockModel> stocks2 = ListTool.cast(stocks1,StockModel.class);
        Collection<MoneyLogCollectionModel<MoneyLogModel>> logs = MoneyLogCollectionModel.makeCollection(ListTool.cast(stocks2,MoneyLogModel.class));
        model.addAttribute(ThymleafVarName.Var.moneyDetail,logs);
        return "currency/stock";
    }//*/
}
